package test;

import domain.Article;
import mapper.ArticalMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class test {
    //查找所有的文章信息以及其关联的评论信息
    @Test
    public  void test1() throws IOException {
        //1.加载核心配置文件
        InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");
        //2. 获取sqlSessionFactory工厂对象
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
        //3. 获取sqlSession会话对象
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //4.当前返回的 其实是基于ArticalMapper所产生的代理对象：底层：JDK动态代理 实际类型：proxy
        ArticalMapper mapper = sqlSession.getMapper(ArticalMapper.class);
        List<Article> articles = mapper.findAll();
        //5. 遍历打印结果
        for (Article article : articles) {
            System.out.println(article);
            System.out.println(article.getComments());
        }
        //6. 关闭资源
        sqlSession.close();
    }
}
